//
//  HWFriendViewController.m
//  HomeWork
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HWFriendViewController.h"
#import "HWUserInfo.h"
#import "HWTweetsModel.h"
#import <MJRefresh/MJRefresh.h>
#import "HWFriendHeaderV.h"
#import "HWTweetDBTool.h"
#import "HWTweetCell.h"

#import "HWFriendHomeViewModel.h"
#import "HWDownloader.h"
@interface HWFriendViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 表视图 */
@property (strong, nonatomic) UITableView *tableView;

/** 头部视图 */
@property (strong, nonatomic) HWFriendHeaderV *headerV;
/** viewModel */
@property (strong, nonatomic) HWFriendHomeViewModel *viewModel;
@end

@implementation HWFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindViewModel];
    [self tableView];
    self.title = @"朋友圈";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];;
}

- (void)orientationChange{
    
    [self.view layoutIfNeeded];
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
    [self.viewModel.dataArray makeObjectsPerformSelector:@selector(setRowHeight:) withObject:0];
    [self.tableView reloadData];
    
    
}



- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [HWTweetDBTool deleteAllTweets];
}
#pragma mark - 懒加载

- (HWFriendHomeViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[HWFriendHomeViewModel alloc] init];
    }
    return _viewModel;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.view);
        }];
        HWFriendHeaderV *headerV = [[HWFriendHeaderV alloc] init];
        headerV.frame = CGRectMake(0, 0, kScreenWidth, 400);
        _tableView.tableHeaderView = headerV;
        self.headerV = headerV;
        
        [self setRefresh];
        
    }
    return _tableView;
}

- (void)setRefresh{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.viewModel.refreshDataCommand execute:nil];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.viewModel.nextPageCommand execute:nil];
    }];
}

- (void)bindViewModel{
    
    [self.viewModel.refreshDataCommand execute:nil];
    [self.viewModel.userInfoCommand execute:nil];
    
    @weakify(self);
    
    [self.viewModel.userSubject subscribeNext:^(id x) {
        @strongify(self);
        self.headerV.userInfo = self.viewModel.userInfo;
    }];
    
    [self.viewModel.refreshEndSubject subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
        switch ([x integerValue]) {
            
            case refreshEndStatusNormal: {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer resetNoMoreData];
                [self.tableView.mj_footer endRefreshing];
            }
                break;
            case refreshEndStatusNoMoreData: {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
                break;
            default:
                break;
        }
    }];
    
    [[self.viewModel.commentSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSIndexPath *indexPath) {
        HWLog(@"==点击评论第几行==%ld==", indexPath.row+1);
    }];
    
    [[self.viewModel.cellViewModel.replyCmtSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(HWTweetCommentUser *commentUser) {
        HWLog(@"-用户名--%@--评论内容--%@", commentUser.sender.username, commentUser.content);
    }];
    [[self.viewModel.cellViewModel.imageClickSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
        HWLog(@"==%@", x);
    }];
}

#pragma mark - 数据源 && 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HWTweetCell *cell = [HWTweetCell cellWithTableView:tableView];
    HWTweetsModel *model = [self.viewModel.dataArray objectAtIndex:indexPath.row];
    cell.viewModel = self.viewModel.cellViewModel;
    cell.indexPath = indexPath;
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HWTweetsModel *model = [self.viewModel.dataArray objectAtIndex:indexPath.row];

    return [HWTweetCell heightWithModel:model];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[HWDownloader sharedInstance] cancelAllDownload];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
